package com.d3works.apps.account.service.invoice.workflow.ventilate;

import com.d3works.apps.account.db.repo.InvoiceRepository;
import com.d3works.apps.account.service.app.AppAccountService;
import com.d3works.apps.account.service.config.AccountConfigService;
import com.d3works.apps.account.service.fixedasset.FixedAssetGenerationService;
import com.d3works.apps.account.service.invoice.InvoiceTermService;
import com.d3works.apps.account.service.move.MoveCreateFromInvoiceService;
import com.d3works.apps.base.service.administration.SequenceService;
import com.d3works.apps.base.service.user.UserService;
import com.d3works.exception.D3worksException;
import com.google.common.base.Preconditions;
import com.google.inject.Inject;
import com.google.inject.servlet.RequestScoped;

@RequestScoped
public class VentilateAdvancePaymentState extends VentilateState {

  @Inject
  public VentilateAdvancePaymentState(
      SequenceService sequenceService,
      MoveCreateFromInvoiceService moveCreateFromInvoiceService,
      AccountConfigService accountConfigService,
      AppAccountService appAccountService,
      InvoiceRepository invoiceRepo,
      WorkflowVentilationService workflowService,
      UserService userService,
      FixedAssetGenerationService fixedAssetGenerationService,
      InvoiceTermService invoiceTermService) {
    super(
        sequenceService,
        moveCreateFromInvoiceService,
        accountConfigService,
        appAccountService,
        invoiceRepo,
        workflowService,
        userService,
        fixedAssetGenerationService,
        invoiceTermService);
  }

  @Override
  public void process() throws D3worksException {

    Preconditions.checkNotNull(invoice.getPartner());

    setDate();
    setJournal();
    setPartnerAccount();
    setInvoiceId();
    updatePaymentSchedule();
    // we don't create the move
    // and the invoice stays validated

    workflowService.afterVentilation(invoice);
  }
}
